﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace Epitech.Krisk.Xna.ScreenManagerLib.Other
{
    public class ScreenResolution
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private struct DEVMODE
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmDeviceName;
            public short dmSpecVersion;
            public short dmDriverVersion;
            public short dmSize;
            public short dmDriverExtra;
            public int dmFields;
            public short dmOrientation;
            public short dmPaperSize;
            public short dmPaperLength;
            public short dmPaperWidth;
            public short dmScale;
            public short dmCopies;
            public short dmDefaultSource;
            public short dmPrintQuality;
            public short dmColor;
            public short dmDuplex;
            public short dmYResolution;
            public short dmTTOption;
            public short dmCollate;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmFormName;
            public short dmLogPixels;
            public int dmBitsPerPel;
            public int dmPelsWidth;
            public int dmPelsHeight;
            public int dmDisplayFlags;
            public int dmDisplayFrequency;
        }

        public List<String> ScreenResolutions { get; set; }

        public ScreenResolution()
        {
            int cpt = 0;
            DEVMODE dev = new DEVMODE();
            ScreenResolutions = new List<string>();

            dev.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            while (EnumDisplaySettings(null, cpt, ref dev))
            {
                ScreenResolutions.Add(String.Format("{0}x{1}", dev.dmPelsWidth, dev.dmPelsHeight));
                cpt++;
            }
            ScreenResolutions = ScreenResolutions.Distinct().ToList();
        }
    }
}
